<template>
  <div class="update-name">
    <van-nav-bar
      title="修改昵称"
      left-text="取消"
      right-text="确定"
      @click-left="$emit('close')"
      @click-right="onConfrim"
    ></van-nav-bar>
    <div class="field-wrap">
      <van-field
        v-model="message"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入新昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      message: this.value
    }
  },
  methods: {
    async onConfrim() {
      // 加载中
      this.$toast.loading({
        message: '保存中',
        forbidClick: false,
        duration: 0
      })
      // 请求修改数据
      try {
        const locaName = this.message
        console.log(locaName)
        if (!locaName.length) return this.$toast('请输入内容')
        await this.$api.updateUSerProfile({
          name: locaName
        })
        // 更新视图
        this.$emit('input', locaName)
        // 关闭弹层
        this.$emit('close')
        // 提示信息
        this.$toast('更新成功')
      } catch (err) {
        console.log(err)
        this.$toast('更新失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.field-wrap {
  padding: 10px;
}
.van-field {
  background: #f5f7f9;
}
</style>
